<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class Mod_employee extends PNC_Model {

	public function __construct() {
		parent::__construct();
	}

	/*
	 * Login
	 * @param string
	 * @param string
	 * @access public
	 * @return array
	 */

	public function login($username, $password) {
//                $username = "Sao.Lina";
//                $password = "1234567";
		$user_log = $this
				->db
				->select('*')
				->from('pnc_employee')
				->join('pnc_roles', 'pnc_roles.role_id = pnc_employee.role_id')
				->join('pnc_position', 'pnc_position.pos_id = pnc_employee.pos_id')
				->where('pnc_employee.username', $username)
				->where('pnc_employee.password', md5($password))
				->where('pnc_employee.emp_status', 1)
				->get();
		return $user_log;
	}

	public function count_employee() {
		return $this->db->count_all("pnc_employee");
	}

	public function show_employee($per_page, $offset, $id) {
		$query = $this
				->db
				->select('*')
				->from('pnc_employee')
				->join('pnc_department', 'pnc_employee.dept_id = pnc_department.dept_id')
				->join('pnc_roles', 'pnc_roles.role_id = pnc_employee.role_id')
				->join('pnc_position', 'pnc_position.pos_id = pnc_employee.pos_id')
//				->join("pnc_group", "pnc_group.grp_id = pnc_employee.grp_id")
				->order_by('emp_id', 'ASC')
				->limit($per_page, $offset)
				->where('emp_status', 1)
				->get();
		if ($query->num_rows() > 0) {
			foreach ($query->result() as $row_employee) {
				$data[] = $row_employee;
			}
			return $data;
		}
		return FALSE;
	}

	public function get_employee() {
		$query = $this
				->db
				->select('*')
				->from('pnc_employee')
				->join('pnc_department', 'pnc_employee.dept_id = pnc_department.dept_id')
				->join('pnc_roles', 'pnc_roles.role_id = pnc_employee.role_id')
//				->join("pnc_group", "pnc_group.grp_id = pnc_employee.grp_id")
				->order_by("emp_fname", "ASC")
				->where('emp_status', 1)
				->get();
		if ($query->num_rows() > 0) {
			foreach ($query->result() as $row_employee) {
				$data[] = $row_employee;
			}
			return $data;
		}
		return FALSE;
	}

	public function get_admin($id) {
		$query = $this->db
				->select('*')
				->from('pnc_roles')
				->join('pnc_employee', 'pnc_roles.role_id = pnc_employee.role_id')
				->where('pnc_roles.role_alias', 'admin')
				->where('pnc_employee.staff_id', $id)
				->where('pnc_employee.emp_status', 1)
				->get();
		if ($query->num_rows() > 0) {
			foreach ($query->result() as $staff) {
				$data[] = $staff;
			}
			return $data;
		}
		return FALSE;
	}

	public function get_staff($id) {
		$query = $this->db
				->select('*')
				->from('pnc_employee')
				->where('pnc_employee.emp_status', 1)
				->where('staff_id', $id)
				->get();
		if ($query->num_rows() > 0) {
			foreach ($query->result() as $staff) {
				$data[] = $staff;
			}
			return $data;
		}
		return FALSE;
	}

	public function set_validator($id) {
		$query = $this->db
				->select('emp_fname, emp_lname')
				->from('pnc_record')
				->join('pnc_employee', 'pnc_record.staff_id = pnc_employee.staff_id')
				->where('pnc_record.rcd_id', $id)
				->get();
		foreach ($query->result_array() as $row) {
			$set_val[] = $row['emp_fname'] . ' ' . $row['emp_lname'];
		}
		return $set_val;
	}
/* Add new employee */
	public function add_employee($get_emp_id, $get_fname, $get_lname, $get_username, $get_password, $get_email, $get_gender, $get_position, $get_role, $get_create_date, $get_status, $get_department, $image, $get_sub_dept, $get_start_workdate) {
		$password = md5($get_password);
		$data = array(
			'emp_id' => $get_emp_id,
			'emp_fname' => $get_fname,
			'emp_lname' => $get_lname,
			'username' => $get_username,
			'password' => $password,
			'emp_email' => $get_email,
			'emp_gender' => $get_gender,
//			'grp_id' => $get_group,
			'pos_id' => $get_position,
			'role_id' => $get_role,
			'date_created' => $get_create_date,
			'start_date_work' => $get_start_workdate,
			'emp_status' => $get_status,
			'emp_photo' => $image,
			'dept_id' => $get_department,
			'sub_dept' => $get_sub_dept
		);
		$this->db->insert('pnc_employee', $data);
		return $this->db->affected_rows() > 0;
	}
/* Get info of employee via id */
	public function edit_employee($id) {
		$query = $this->db
				->select('*')
				->from('pnc_employee')
				->join('pnc_roles', 'pnc_roles.role_id = pnc_employee.role_id')
				->join('pnc_position', 'pnc_position.pos_id = pnc_employee.pos_id')
				->join('pnc_department', 'pnc_department.dept_id = pnc_employee.dept_id')
//				->join('pnc_group', 'pnc_group.grp_id = pnc_employee.grp_id')
				->where('pnc_employee.staff_id', $id)
				->get();
		return $query;
	}
/* Update employee */
	public function update_employee($id, $get_emp_id, $get_fname, $get_lname, $get_email, $get_username, $get_gender, $get_position, $get_role, $get_status, $get_department, $get_modified_date, $image, $get_sub_dept, $get_start_workdate) {
		$data = array(
			'emp_id' => $get_emp_id,
			'emp_fname' => $get_fname,
			'emp_lname' => $get_lname,
			'emp_email' => $get_email,
			'username' => $get_username,
			'emp_gender' => $get_gender,
//			'grp_id' => $get_group,
			'pos_id' => $get_position,
			'role_id' => $get_role,
			'date_modified' => $get_modified_date,
			'start_date_work' => $get_start_workdate,
			'emp_status' => $get_status,
			'emp_photo' => $image,
			'dept_id' => $get_department,
			'sub_dept' => $get_sub_dept
		);
		$this->db->where('staff_id', $id);
		return $this->db->update('pnc_employee', $data);
	}

	public function delete_individual($id, $where, $table) {
		$this->db->where($where, $id);
		return $this->db->delete($table);
	}

}

/* End of file mod_employee.php */
/* Location: ./application/models/mod_employee.php */